package com.richrail.gui;

import java.awt.Component;
import java.util.List;

import javax.swing.JTextPane;

import com.richrail.Depot;
import com.richrail.state.DepotState;
import com.richrail.state.TrainState;
import com.richrail.state.WagonState;
import com.richrail.util.Observable;

public class TextView extends JTextPane implements View
{
	private static final long serialVersionUID = 1L;
	
	private DepotState depotState;
	
	public void setDepotState(DepotState depotState)
	{
		this.depotState = depotState;
	}
	
	public void resolveText()
	{
		if (depotState != null)
		{
			String stateText = "";
			
			List<WagonState> wagonStates = depotState.getWagons();
			if (wagonStates.size() > 0)
			{
				stateText += "Wagons:\n";
				for (WagonState wagonState: wagonStates)
				{
					stateText += "(" + wagonState.getName() + ":" + wagonState.getSeatCount() + ") ";
				}
				stateText += "\n";
			}
			
			List<TrainState> trainStates = depotState.getTrains();
			if (trainStates.size() > 0)
			{
				stateText += "Trains:\n";
				for (TrainState trainState: depotState.getTrains())
				{
					stateText += "(" + trainState.getName() + ":" + trainState.getSeatCount() + ")";
					for (WagonState wagonState: trainState.getWagons())
					{
						stateText += "-(" + wagonState.getName() + ")";
					}
					stateText += "\n";
				}
			}
			
			setText(stateText);
		}
	}

	@Override
	public void update(Observable observable)
	{
		depotState = ((Depot) observable).getState();
		resolveText();
	}

	@Override
	public String getName()
	{
		return "Textual";
	}
	
	@Override
	public Component toComponent()
	{
		return this;
	}
}
